<?php

namespace App\Service\Cruise\CruiseProject;

use App\Enums\BaseErrorCode;
use App\Exceptions\Custom\ResponseHttpException;
use App\Models\Cruise\CruiseTaskProjectModel;
use App\Support\CustomForm\AttributeCruiseSupport;
use App\Support\CustomForm\AttributeSupport;
use App\Support\CustomForm\ValidateSupport;

class CruiseProjectInputService
{
    private AttributeCruiseSupport $cruiseSupport;

    private string $inputType;
    private bool   $required         = true;
    private array  $options          = [];
    private int    $maxContentLength = 0;
    private string $richShow = '';
    private int    $meanType = 0;
    private int    $projectId = 0;

    public function __construct(string $inputType, string $inputName, string $inputDesc)
    {
        $this->inputType     = $inputType;
        $this->cruiseSupport = AttributeCruiseSupport::getInstance($inputName, $inputType, $inputDesc);
    }

    public function setMeanType(int $meanType): self
    {
        $this->meanType = $meanType;
        return $this;
    }

    public function setProjectId(int $projectId): self
    {
        $this->projectId = $projectId;
        return $this;
    }

    public function setRichShow(string $richShow): self
    {
        $this->richShow = $richShow;
        return $this;
    }

    public function setRequired(bool $required): self
    {
        $this->required = $required;
        return $this;
    }

    public function setOptions(array $options): self
    {
        $this->options = $options;
        return $this;
    }

    public function setMaxContentLength(int $maxContentLength): self
    {
        $this->maxContentLength = $maxContentLength;
        return $this;
    }

    public function prepareAttribute(): AttributeCruiseSupport
    {
        // meanType
        $this->cruiseSupport->setMeanType($this->meanType)
                            ->setProjectId($this->projectId);

        switch ($this->inputType) {
            case AttributeSupport::INPUT_TYPE_SELECT:
            case AttributeSupport::INPUT_TYPE_SELECT_MULTIPLE:
                $this->cruiseSupport->setValidateRules(ValidateSupport::getInstance($this->required))
                                    ->setCustomAttribute(['options', $this->options]);
                break;
            case AttributeSupport::INPUT_TYPE_STRING:
            case AttributeSupport::INPUT_TYPE_TEXT:
            case AttributeSupport::INPUT_TYPE_IMG_GROUP:
            case AttributeSupport::INPUT_VIDEO:
            case AttributeCruiseSupport::INPUT_TYPE_SIGNATURE:
            case AttributeCruiseSupport::INPUT_TYPE_PAYEE_INFO:
            case AttributeCruiseSupport::INPUT_TYPE_RICH_TEXT:
                $this->cruiseSupport->setValidateRules(
                    ValidateSupport::getInstance($this->required)->setContentLength(0, $this->maxContentLength)
                );
                break;
            case AttributeCruiseSupport::INPUT_TYPE_BUSINESS_THREE:
                $this->cruiseSupport->setValidateRules(ValidateSupport::getInstance($this->required))
                                    ->setCustomAttribute(['header', ['日期', '营业额', '营业收入', '订单量']]);
                break;
            case AttributeSupport::INPUT_DATE:
            case AttributeCruiseSupport::INPUT_TYPE_BUSINESS_LICENSE:
            case AttributeCruiseSupport::INPUT_TYPE_DATE_RANGE:
                $this->cruiseSupport->setValidateRules(ValidateSupport::getInstance($this->required));
                break;

            case AttributeCruiseSupport::INPUT_TYPE_CRUISE_CHECK:
            case AttributeCruiseSupport::INPUT_TYPE_CRUISE_CHECK_SKIP:
            case AttributeCruiseSupport::INPUT_TYPE_CRUISE_CHECK_BIOLOGY:
                $this->cruiseSupport->setValidateRules(ValidateSupport::getInstance($this->required))
                                    ->setCustomAttribute(['options', $this->options])
                                    ->setCustomAttribute(['auditValue', []]);
                break;
            case AttributeCruiseSupport::INPUT_TYPE_FEE_DELAY:
                $this->cruiseSupport->setCustomAttribute(['feeNum', 0])
                                    ->setCustomAttribute(['feeUrl', '']);
                break;
            case AttributeCruiseSupport::INPUT_TYPE_RECTIFY_AGAINST:
                $this->cruiseSupport->setCustomAttribute(['againstNum', 0])
                                    ->setCustomAttribute(['againstUrl',''])
                                    ->setCustomAttribute(['rectifyNum', 0])
                                    ->setCustomAttribute(['rectifyUrl', '']);
                break;
            case AttributeCruiseSupport::INPUT_TYPE_IMG_VIDEO:
            case AttributeCruiseSupport::INPUT_TYPE_IMG_FILE:
                $this->cruiseSupport->setValidateRules(ValidateSupport::getInstance($this->required))
                                    ->setCustomAttribute(['modeType', 'h5-generalization'])
                                    ->setCustomAttribute(['fileType', ['pdf', 'doc', 'docx']])
                                    ->setCustomAttribute(['filesCountLimit', 9])
                                    ->setCustomAttribute(['imgCountLimit', 9])
                                    ->setCustomAttribute(['videoCountLimit', 0]);
                break;
            case AttributeCruiseSupport::INPUT_TYPE_APPROVAL_REJECT:
                $this->cruiseSupport->setValidateRules(ValidateSupport::getInstance($this->required))
                                    ->setCustomAttribute([
                                        'options',
                                        [
                                            [
                                                'text'  => '通过',
                                                'value' => AttributeCruiseSupport::CRUISE_CHECK_OK,
                                            ],
                                            [
                                                'text'  => '驳回',
                                                'value' => AttributeCruiseSupport::CRUISE_CHECK_FAIL,
                                            ],
                                        ]
                                    ]);
                break;

            case AttributeCruiseSupport::INPUT_TYPE_UPDATE_KF_SYSTEM:
                $this->cruiseSupport
                    ->setCustomAttribute(['tips', [
                        [
                            'text'    => '更新夸父收银1.0',
                            'column'  => 'kfPos',
                            'isRenew' => false,
                        ],
                        [
                            'text'    => '更新夸父监控1.0',
                            'column'  => 'kfMonitor',
                            'isRenew' => false,
                        ],
                        [
                            'text'    => '更新夸父商屏1.0',
                            'column'  => 'kfScreen',
                            'isRenew' => false,
                        ],
                    ]])
                    ->setCustomAttribute(['options', [
                        [
                            'text' => '我已更新',
                            'value' => AttributeCruiseSupport::CRUISE_CHECK_OK,
                        ],
                        [
                            'text' => '我有问题',
                            'value' => AttributeCruiseSupport::CRUISE_CHECK_FAIL,
                        ],
                    ]])
                    ->setCustomAttribute(['defaultValue', AttributeCruiseSupport::CRUISE_CHECK_OK]);
                break;
            case AttributeCruiseSupport::INPUT_TYPE_IDENTITY_CARD_FRONT:
            case AttributeCruiseSupport::INPUT_TYPE_IDENTITY_CARD_BACK:
                $this->cruiseSupport->setValidateRules(ValidateSupport::getInstance($this->required));

                // 私有公有
                if (in_array($this->meanType, [
                    CruiseTaskProjectModel::MEAN_TYPE_ID_CARD_BACK_PRIVATE,
                    CruiseTaskProjectModel::MEAN_TYPE_ID_CARD_FRONT_PRIVATE
                ])) {
                    $this->cruiseSupport->setCustomAttribute(['isPublic', false]);
                }

                break;

            case AttributeCruiseSupport::INPUT_TYPE_RICH_SHOW:
                $this->cruiseSupport->setCustomAttribute(['richShow', $this->richShow])->setCustomAttribute(['disabled', true]);
                break;
            case AttributeCruiseSupport::INPUT_TYPE_PROVINCE_CITY_AREA:
                $this->cruiseSupport->setValidateRules(ValidateSupport::getInstance($this->required));
                break;
        }

        return $this->cruiseSupport;
    }
}
